<?php
include "include/config.inc.php";
error_reporting(E_ALL^ E_WARNING);
$today = date("Y-m-d");

//on submit update draw table
if(isset($_POST['subBtn']))
{
  $drawId = $_POST['drawId'];
  $cardNo = $_POST['cardNo'];
  $winnerStatus = 1;
  
  //Check Card No 
  $checkCardNo = "SELECT loanDrawId
                    FROM loandraw
                   WHERE loanDrawId = ".$cardNo;
  $checkCardNoRes = mysql_query($checkCardNo);
      
  if($checkCardNoRow = mysql_num_rows($checkCardNoRes) > 0)
  {
    //Check Winner 
    $checkWinner = "SELECT winnerLoanDrawId 
                      FROM draw
                     WHERE winnerLoanDrawId = ".$cardNo;
    $checkWinnerRes = mysql_query($checkWinner);
    if($checkWinnerRow = mysql_num_rows($checkWinnerRes) <= 0)
    {
    	//check if winner installment is panding
      $selectDrawnstallmentNo = "SELECT loanDrawId
                                   FROM drawinstallment 
                                  WHERE drawInstallmentDate < '".$today."'
                                    AND isReceived = 'N'
                                    AND loanDrawId = ".$cardNo."
                               GROUP BY loanDrawId" ;
      $selectDrawnstallmentNoRes = mysql_query($selectDrawnstallmentNo);      
      $selectDrawnstallmentNoRow = mysql_fetch_array($selectDrawnstallmentNoRes);
      if($selectDrawnstallmentNoRow['loanDrawId'] == $cardNo)
      {
        $winner = 1;
      }
      else
      {
      	$winner = 0;
      }
      //Select Sub Winner 1
      $selectSubWinner1 = "SELECT MAX(loanDrawId) AS loanDrawId
                             FROM loandraw 
                            WHERE isWon = 0 
                              AND loanDrawId < ".$cardNo;
      $selectSubWinner1Res = mysql_query($selectSubWinner1);
      $selectSubWinner1Row = mysql_fetch_array($selectSubWinner1Res);
      if($selectSubWinner1Row['loanDrawId'] != NULL)
      {
        $subWinner1 = $selectSubWinner1Row['loanDrawId'];
      }
      else
      {
         $subWinner1 = 0;
      }
      
      //check if subwinner1 installment is panding
      $selectDrawnstallmentNo = "SELECT loanDrawId
                                   FROM drawinstallment 
                                  WHERE drawInstallmentDate < '".$today."'
                                    AND isReceived = 'N'
                                    AND loanDrawId = ".$subWinner1."
                               GROUP BY loanDrawId" ;
      $selectDrawnstallmentNoRes = mysql_query($selectDrawnstallmentNo);      
      $selectDrawnstallmentNoRow = mysql_fetch_array($selectDrawnstallmentNoRes);
      if($selectDrawnstallmentNoRow['loanDrawId'] == $subWinner1)
      {
        $subWinner1status = 1;
      }
      else
      {
      	$subWinner1status = 0;
      }
      //Select Sub Winner 2
      $selectSubWinner2 = "SELECT MAX(loanDrawId) AS loanDrawId  
                             FROM loandraw 
                            WHERE isWon = 0 
                              AND loanDrawId < ".$subWinner1;
      $selectSubWinner2Res = mysql_query($selectSubWinner2);
      $selectSubWinner2Row= mysql_fetch_array($selectSubWinner2Res);
      if($selectSubWinner2Row['loanDrawId'] != NULL)
      {
        $subWinner2 = $selectSubWinner2Row['loanDrawId'];
      }
      else
      {
         $subWinner2 = 0;
      }
      //check if subwinner2 installment is panding
      $selectDrawnstallmentNo2 = "SELECT loanDrawId
                                    FROM drawinstallment 
                                   WHERE drawInstallmentDate < '".$today."'
                                     AND isReceived = 'N'
                                     AND loanDrawId = ".$subWinner2."
                                GROUP BY loanDrawId" ;
      $selectDrawnstallmentNo2Res = mysql_query($selectDrawnstallmentNo2);      
      $selectDrawnstallmentNo2Row = mysql_fetch_array($selectDrawnstallmentNo2Res);
      if($selectDrawnstallmentNo2Row['loanDrawId'] == $subWinner2)
      {
        $subWinner2status = 1;
      }
      else
      {
      	$subWinner2status = 0;
      }
      //Select Sub Winner 3
      $selectSubWinner3 = "SELECT MIN(loanDrawId) AS loanDrawId  
                             FROM loandraw 
                            WHERE isWon = 0 
                              AND loanDrawId > ".$cardNo;
      $selectSubWinner3Res = mysql_query($selectSubWinner3);
      $selectSubWinner3Row = mysql_fetch_array($selectSubWinner3Res);
      if($selectSubWinner3Row['loanDrawId'] != NULL)
      {
        $subWinner3 = $selectSubWinner3Row['loanDrawId'];
      }
      else
      {
         $subWinner3 = 0;
      }
      //check if subwinner1 installment is panding
      $selectDrawnstallmentNo3 = "SELECT loanDrawId
                                    FROM drawinstallment 
                                   WHERE drawInstallmentDate < '".$today."'
                                     AND isReceived = 'N'
                                     AND loanDrawId = ".$subWinner3."
                                GROUP BY loanDrawId" ;
      $selectDrawnstallmentNo3Res = mysql_query($selectDrawnstallmentNo3);      
      $selectDrawnstallmentNo3Row = mysql_fetch_array($selectDrawnstallmentNo3Res);
      if($selectDrawnstallmentNo3Row['loanDrawId'] == $subWinner3)
      {
        $subWinner3status = 1;
      }
      else
      {
      	$subWinner3status = 0;
      }
      //Select Sub Winner 4
      $selectSubWinner4 = "SELECT MIN(loanDrawId) AS loanDrawId  
                             FROM loandraw 
                            WHERE isWon = 0 
                              AND loanDrawId > ".$subWinner3;
      $selectSubWinner4Res = mysql_query($selectSubWinner4);
      $selectSubWinner4Row = mysql_fetch_array($selectSubWinner4Res);
      if($selectSubWinner4Row['loanDrawId'] != NULL)
      {
        $subWinner4 = $selectSubWinner4Row['loanDrawId'];
      }
      else
      {
         $subWinne4 = 0;
      }
      //check if subwinner1 installment is panding
      $selectDrawnstallmentNo4 = "SELECT loanDrawId
                                    FROM drawinstallment 
                                   WHERE drawInstallmentDate < '".$today."'
                                     AND isReceived = 'N'
                                     AND loanDrawId = ".$subWinner4."
                                GROUP BY loanDrawId" ;
      $selectDrawnstallmentNo4Res = mysql_query($selectDrawnstallmentNo4);      
      $selectDrawnstallmentNo4Row = mysql_fetch_array($selectDrawnstallmentNo4Res);
      if($selectDrawnstallmentNo4Row['loanDrawId'] == $subWinner4)
      {
        $subWinner4status = 1;
      }
      else
      {
      	$subWinner4status = 0;
      }
      //Update Winner Status in Draw Table
      $updateDraw = "UPDATE draw 
                        SET winnerLoanDrawId        = '".$cardNo."',
                            winnerLoanDrawIdStatus  = '".$winner."',
                            winnerLoanDrawId1       = '".$subWinner1."',
                            winnerLoanDrawId1Status = '".$subWinner1status."',
                            winnerLoanDrawId2       = '".$subWinner2."',
                            winnerLoanDrawId2Status = '".$subWinner2status."',
                            winnerLoanDrawId3       = '".$subWinner3."', 
                            winnerLoanDrawId3Status = '".$subWinner3status."',
                            winnerLoanDrawId4       = '".$subWinner4."',
                            winnerLoanDrawId4Status = '".$subWinner4status."'
                      WHERE drawId = ".$drawId;
      $updateDrawRes = mysql_query($updateDraw);
      
      //Update Winner is isWon in Loan Draw Table
      $updateLoanDraw ="UPDATE loandraw 
                           SET isWon = 1
                         WHERE loanDrawId =".$cardNo;
      $updateLoanDrawRes = mysql_query($updateLoanDraw);
      $deleteDrawInstallment= "DELETE FROM drawinstallment 
                                WHERE isReceived = 'N' 
                                  AND loanDrawId = ".$cardNo;
      $deleteDrawInstallmentRes = mysql_query($deleteDrawInstallment);
      
      
      //All Winner Status Update
      
      
      //Update Null Collum
      $upd    = "UPDATE draw
                    SET winnerLoanDrawId1 = 0,
                        winnerLoanDrawId2 = 0
                  WHERE winnerLoanDrawId1 = ''
                    AND winnerLoanDrawId2 = ''";
      $updRes = mysql_query($upd);
      $upd1    = "UPDATE draw
                    SET winnerLoanDrawId4 = 0
                  WHERE winnerLoanDrawId4 = ''";
      $updRes1 = mysql_query($upd1);
      $upd2    = "UPDATE draw
                    SET winnerLoanDrawId3 = 0
                  WHERE winnerLoanDrawId3 = ''";
      $updRes2 = mysql_query($upd2);
    }
    else
    {
      echo "<script> alert('This Card No. is Aleready Winner.'); </script>";
    }
  }
  else
  {
    echo "<script> alert('This Card No. is Not Avaliable.'); </script>";	
  }
}   
$NextDrawArray = array();
$i = 0 ;
$j = 0 ;
$selNextDraw = "SELECT MIN( drawId ) AS drawId, winnerLoanDrawId, drawDate, prize
                  FROM draw
                 WHERE winnerLoanDrawId = '0'";
$selNextDrawRes = mysql_query($selNextDraw) or die("Error...".mysql_error());
while($selNextDrawRow=mysql_fetch_array($selNextDrawRes))
{
  $nextDrawArray['drawId']   = $selNextDrawRow['drawId'];
  $nextDrawArray['drawDate'] = date("d-m-Y", strtotime($selNextDrawRow['drawDate']));
  $nextDrawArray['prize']    = $selNextDrawRow['prize'];
  $i++;
}

//Display All Winner 
$winnerArray = array();
$k = 0;
$selectWinner = "SELECT drawId, winnerLoanDrawId, winnerLoanDrawIdStatus, 
                                winnerLoanDrawId1, winnerLoanDrawId1Status,
                                winnerLoanDrawId2, winnerLoanDrawId2Status,
                                winnerLoanDrawId3, winnerLoanDrawId3Status,
                                winnerLoanDrawId4, winnerLoanDrawId4Status, drawDate, prize
                   FROM draw
                  WHERE winnerLoanDrawId != '0'";
$selectWinnerRes = mysql_query($selectWinner);
while($selectWinnerRow=mysql_fetch_array($selectWinnerRes))
{
  $winnerArray[$k]['drawId']                  = $selectWinnerRow['drawId'];
  $winnerArray[$k]['winnerLoanDrawId']        = $selectWinnerRow['winnerLoanDrawId'];
  $winnerArray[$k]['winnerLoanDrawId1']       = $selectWinnerRow['winnerLoanDrawId1'];
  $winnerArray[$k]['winnerLoanDrawId2']       = $selectWinnerRow['winnerLoanDrawId2'];
  $winnerArray[$k]['winnerLoanDrawId3']       = $selectWinnerRow['winnerLoanDrawId3'];
  $winnerArray[$k]['winnerLoanDrawId4']       = $selectWinnerRow['winnerLoanDrawId4'];
  $winnerArray[$k]['winnerLoanDrawIdStatus']  = $selectWinnerRow['winnerLoanDrawIdStatus'];
  $winnerArray[$k]['winnerLoanDrawId1Status'] = $selectWinnerRow['winnerLoanDrawId1Status'];
  $winnerArray[$k]['winnerLoanDrawId2Status'] = $selectWinnerRow['winnerLoanDrawId2Status'];
  $winnerArray[$k]['winnerLoanDrawId3Status'] = $selectWinnerRow['winnerLoanDrawId3Status'];
  $winnerArray[$k]['winnerLoanDrawId4Status'] = $selectWinnerRow['winnerLoanDrawId4Status'];
  $winnerArray[$k]['drawDate']                = $selectWinnerRow['drawDate'];
  $winnerArray[$k]['prize']                   = $selectWinnerRow['prize'];
  $k++;
}

$smarty->assign('nextDrawArray',$nextDrawArray);
$smarty->assign('winnerArray',$winnerArray);
$smarty->display('drawWinner.tpl');
?>